import cv2

# face_detector = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface_alt_tree.xml')
# face_detector = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalcatface.xml')
face_detector = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface_alt.xml')


time = 0


def face_detect_demo(src):
    global time

    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

    faces = face_detector.detectMultiScale(gray, 1.05, 5)

    for x, y, w, h in faces:
        time += 1
        name = './data/03/' + ('0' + str(time) if time < 10 else str(time)) + '.jpg'
        print(name)
        cv2.imwrite(name, src[y: y+h, x: x+w])
        cv2.rectangle(src, (x, y), (x + w, y + h), (0, 0, 255), thickness=2)

    cv2.imshow('result', src)


cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture('assets/video/2.mp4')


while True:
    flag, frame = cap.read()
    face_detect_demo(frame)

    if ord('q') == cv2.waitKey(10):
        break

    if time == 100:
        break

cv2.destroyAllWindows()
cap.release()
